package com.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mall.dao.EvalutionMapper;
import com.mall.entity.Evaluation;
import com.mall.entity.User;
import com.mall.service.EvalutionService;
import com.mall.utils.UserCache;
import com.mall.vo.WebResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class EvalutionServicelmpl implements EvalutionService {
    @Autowired
    EvalutionMapper evalutionMapper;
    @Override
    public WebResult insertEva(Evaluation evaluation) {
        User user = UserCache.get();
        evaluation.setUsername(user.getUsername());
        try {
            evalutionMapper.insert(evaluation);
            return WebResult.builder().code(200).message("评论添加成功").build();
        } catch (Exception e) {
            e.printStackTrace();
            return WebResult.builder().code(703).message("评论添加失败").build();
        }

    }

    @Override
    public WebResult deleteByPrimaryKey(String evaId) {
        QueryWrapper<Evaluation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",evaId);
        try {
            evalutionMapper.delete(queryWrapper);
            return WebResult.builder().code(200).message("评论删除成功").build();
        } catch (Exception e) {
            return WebResult.builder().code(705).message("评论删除失败").build();
        }
    }

    @Override
    public WebResult selectListBySkuId(Long skuid) {
        QueryWrapper<Evaluation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("skuid",skuid);
        queryWrapper.orderByDesc("eva_date");
        try {
            List<Evaluation> evaluations = evalutionMapper.selectList(queryWrapper);
            return WebResult.builder().code(200).data(evaluations).message("查询成功").build();
        } catch (Exception e) {
            return WebResult.builder().code(707).message("查询失败").build();
        }


    }
}
